package com.ruoyi.web.controller.Base;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.Ability_ServiceLimitingInfor;
import com.ruoyi.domain.Ability_SubscribeInfor;
import com.ruoyi.domain.Ability_UserLimitingInfor;
import com.ruoyi.service.Ability_AbiltyService;
import com.ruoyi.service.Ability_ServiceLimitingService;
import com.ruoyi.service.Ability_UserLimitingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/throttling")
public class ThrottlingController extends BaseController {
    @Autowired
    Ability_ServiceLimitingService  abilityServiceLimitingService;

    @Autowired
    Ability_UserLimitingService ability_UserLimitingService;

    @GetMapping("/serlist")
    public TableDataInfo throttlingList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, // 添加默认值可选
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(required = false,value = "sname") String sname) {
     startPage();
     Ability_ServiceLimitingInfor ability_ServiceLimitingInfor = new Ability_ServiceLimitingInfor();
     ability_ServiceLimitingInfor.setSname(sname);
        return getDataTable(abilityServiceLimitingService.selectAbility_ServiceLimitingInforList(ability_ServiceLimitingInfor));


    }
    @PostMapping("/serupdate")
    public AjaxResult updateThrottling(@RequestBody Ability_ServiceLimitingInfor ability_ServiceLimitingInfor){
        int k=abilityServiceLimitingService.updateAbility_ServiceLimitingInfor(ability_ServiceLimitingInfor);
        if (k>0){
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }

    }

@PostMapping("/add")
    public AjaxResult insertThrottling(@RequestBody Ability_UserLimitingInfor ability_ServiceLimitingInfor){
        int k=ability_UserLimitingService.insertAbility_UserLimitingInfor(ability_ServiceLimitingInfor);
      if(k>0){
          return AjaxResult.success("添加成功");
      }else if(k==-2){
          return AjaxResult.error("添加失败");
      }else if(k==-1){
          return AjaxResult.error("添加失败(可能已经存在该节流)");
      }
        return AjaxResult.success("添加成功");
    }

    @GetMapping("/list")
    public TableDataInfo listThrottling(@RequestParam(required = false,value = "acount") String acount,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, // 添加默认值可选
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
       startPage();
       Ability_UserLimitingInfor ability_UserLimitingInfor = new Ability_UserLimitingInfor();
       ability_UserLimitingInfor.setAcount(acount);
       return getDataTable(ability_UserLimitingService.selectAbility_UserLimitingInforList(ability_UserLimitingInfor));

    }

    @PostMapping("/update")
    public AjaxResult updateThrottling(@RequestBody Ability_UserLimitingInfor ability_UserLimitingInfor){
        int k=ability_UserLimitingService.updateAbility_UserLimitingInfor(ability_UserLimitingInfor);
       System.out.println(k);
        if (k>0){
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }

    }
    @GetMapping("/getThrottling")
    public AjaxResult getThrottling(@RequestParam(value = "sid") String sid){
        return AjaxResult.success(abilityServiceLimitingService.selectAbility_ServiceLimitingInforBySid(sid));
    }


}
